import { defineStore } from 'pinia';
import { getReadListAndStatus, getDepartmentMsgList } from '@/api/dep_msg.js';

// `read_list` 未读列表
export const useMsg = defineStore('messageinfor',{
  state: () => {
    return {
      read_list: [],
      msg_list: []
    }
  },
  actions: {
    async returnReadList(id){ 
      this.read_list = []
      this.msg_list = []
      // select read_list, read_status from users where id = ?
      const res = await getReadListAndStatus(id) 
      // this.read_list = JSON.parse(res.data[0].read_list) 
      this.read_list = JSON.parse(res.data[0].read_list)
      // select * from message where message_receipt_object = ? and message_status = 0 
      // 仅包含当前用户所属部门的未读消息
      const res2 = await getDepartmentMsgList(localStorage.getItem('department'))   
      this.msg_list = res2.data
    }
  },
  persist: true
})